/**
 * Project Name:scb_sms
 * File Name:TreeUtil.java
 * Package Name:com.scb.dev.sms.util.tree
 * Date:Nov 8, 201810:42:55 AM
 * Copyright (c) 2018, Y-Rainson All Rights Reserved.
 *
 */
package com.scb.dev.mms.util.tree;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


 
/**
 * ClassName: TreeUtil <br/>
 * Description: 
 * date: Nov 8, 2018 10:42:55 AM <br/>
 *
 * @author Y-Rainson
 * @version V1.0
 * @since JDK 1.8
 */



/**
 * 树形结构工具类
 *
 * 将一组list对象转成树形结构
 * 该list需符合设定的字段类型
 *
 */
public class TreeUtil {
 
 
    public static Map<String,Object> mapArray = new LinkedHashMap<String, Object>();
 
    public List<TreeNode> menuCommon;
    public List<Object> list = new ArrayList<Object>();
 
    public List<Object> treeMenu(List<TreeNode> menu){
        this.menuCommon = menu;
        for (TreeNode treeNode : menu) {
            Map<String,Object> mapArr = new LinkedHashMap<String, Object>();
            if(treeNode.getParentId().isEmpty()){
                setTreeMap(mapArr,treeNode);
                list.add(mapArr);
            }
        }
        return list;
    }
 
    public List<?> menuChild(String id){
        List<Object> lists = new ArrayList<Object>();
        for(TreeNode a:menuCommon){
            Map<String,Object> childArray = new LinkedHashMap<String, Object>();
            if(a.getParentId() .equals(id)){
               setTreeMap(childArray,a);
               lists.add(childArray);
            }
        }
        return lists;
    }
 
    private void setTreeMap(Map<String,Object> mapArr,TreeNode treeNode){
        mapArr.put("id", treeNode.getId());
        mapArr.put("name", treeNode.getName());
        mapArr.put("parentId", treeNode.getParentId());
        mapArr.put("url", treeNode.getUrl());
        List<?> childrens = menuChild(treeNode.getId());
        if(childrens.size()>0){
            mapArr.put("hasChild",true);
        }
        else{
            mapArr.put("hasChildren",false);
        }
        mapArr.put("childrens", menuChild(treeNode.getId()));
    }
 

 
}
